package com.neusoft.service.impl;

import com.neusoft.mapper.ClientWalletMapper;
import com.neusoft.mapper.CreditBillMapper;
import com.neusoft.service.CreditBillService;
import com.neusoft.service.PenaltyService;
import com.neusoft.domain.VirtualWallet;
import com.neusoft.domain.VirtualWalletPO;
import com.neusoft.exception.InsufficientFundsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;

@Service
public class CreditBillServiceImpl implements CreditBillService {
    @Autowired
    private CreditBillMapper creditBillMapper;

    @Autowired
    private ClientWalletMapper walletClientMapper;
    @Autowired
    private PenaltyService penaltyService;

    @Override
    @Transactional  // 事务管理
    public void createBill(Long walletId, BigDecimal amount) throws InsufficientFundsException {
        VirtualWalletPO walletPO = walletClientMapper.findById(walletId);
        VirtualWallet wallet = convertToVirtualWallet(walletPO);

        // 检查可用信用额度是否足够
        if (wallet.availableCredit().compareTo(amount) < 0) {
            throw new InsufficientFundsException("可用信用额度不足");
        }

        // 使用信用额度消费
        wallet.useCredit(amount);
        walletClientMapper.updateWallet(walletId, wallet.getBalance(), wallet.getUsedCredit());

        // 生成账单
        Date dueDate = java.sql.Date.valueOf(LocalDate.now().plusMonths(1)); // 账单到期时间为一个月后
        creditBillMapper.insertNewBill(walletId, amount, dueDate);
    }

    @Override
    public BigDecimal getOutstandingBill(Long walletId) {
        return creditBillMapper.findOutstandingBill(walletId);
    }

    @Override
    @Transactional  // 事务管理
    public void payBill(Long walletId, BigDecimal paymentAmount) {
        BigDecimal outstandingBill = getOutstandingBill(walletId);
        VirtualWalletPO walletPO = walletClientMapper.findById(walletId);
        VirtualWallet wallet = convertToVirtualWallet(walletPO);

        // 获取账单的到期日期
        Date dueDate = creditBillMapper.findBillDueDate(walletId); // 获取到期日期
        Date currentDate = new Date();

        // 处理逾期情况
        if (outstandingBill.compareTo(BigDecimal.ZERO) > 0 && currentDate.after(dueDate)) {
            // 检查逾期并应用滞纳金
            penaltyService.applyPenalty(walletId, outstandingBill);
        }

        if (paymentAmount.compareTo(outstandingBill) >= 0) {
            creditBillMapper.markBillAsPaid(walletId);
            wallet.repayCredit(outstandingBill);  // 清除已用信用额度
        } else {
            creditBillMapper.updateBillPartialPayment(walletId, paymentAmount);
            wallet.repayCredit(paymentAmount);  // 部分还款，减少已用信用额度
        }

        // 更新数据库中的信用额度信息
        walletClientMapper.updateWallet(walletId, wallet.getBalance(), wallet.getUsedCredit());
    }


    private VirtualWallet convertToVirtualWallet(VirtualWalletPO walletPO) {
        return new VirtualWallet(
                walletPO.getId(),
                walletPO.getUserId(),
                walletPO.getBalance(),
                walletPO.getCreditLimit(),
                walletPO.getUsedCredit(),
                walletPO.getPenaltyAmount()
        );
    }
}
